//创建event
var _customEvent = document.createEvent('Event');
_customEvent.initEvent('HtmlToContentEvent', true, true);

//promise对象的resolve函数，用于同步
var _resolveFunc;

//接收content返回的event，通知await
document.addEventListener('ContentToHtmlEvent', function() {
  var resultData = localStorage.getItem("RESULT");
  _resolveFunc(resultData);
});


//被html调用，向content发送event
function sendEventToContentJs(data, callback) {
  var promise = new Promise(function(resolve, reject){
    _resolveFunc = resolve;
    var jsonData = JSON.stringify(data);
    //alert(jsonData);

    localStorage.setItem("PRN_FUNC", jsonData);
    document.dispatchEvent(_customEvent);
  });
  promise.then(function(value){
    //alert(value);
    callback(JSON.parse(value));
  });
  return promise;
}

/******** sdk 函数 *************/

//PRN_GetDllVersion(char* pbuf, int* psize);
export const PRN_GetDllVersion = (callback) => {
  return sendEventToContentJs({"func":"PRN_GetDllVersion"}, callback);
}

//PRN_ConnectDriver(char* printerName);
export const PRN_ConnectDriver = (printerName, callback) => {
  return sendEventToContentJs({"func":"PRN_ConnectDriver", "para1": printerName}, callback);
}

//PRN_ConnectUsbPort (char* portName, int readTimeOut);
export const PRN_ConnectUsbPort = (portName, timeout, callback) => {
  return sendEventToContentJs({"func":"PRN_ConnectUsbPort", "para1":portName, "para2":timeout }, callback);
}

//PRN_SendBuffer (char* data);
export const PRN_SendBuffer = (data, callback) => {
  return sendEventToContentJs({"func":"PRN_SendBuffer", "para1": data}, callback);
}

//PRN_CloseConnect(void);
export const PRN_CloseConnect = ( callback) => {
  return     sendEventToContentJs({"func":"PRN_CloseConnect"}, callback);
}

//PRN_AddTextToLabel (char* textToPrint, int posX, int posY);
export const PRN_AddTextToLabel = (text, x, y, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddTextToLabel", "para1":text, "para2":x, "para3":y }, callback);

}

//PRN_AddTextToLabelEx (char* textToPrint, char* fontName,int fontSize, int posX, int posY, int dir, int align);
export const PRN_AddTextToLabelEx = (textToPrint, fontName, fontSize, posX, posY, dir, align, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddTextToLabel"
      , "para1":textToPrint, "para2":fontName, "para3":fontSize,"para4":posX, "para5":posY, "para6":dir  , "para7":align
    }
    , callback);
}

//PRN_IndBarcodeFont (char* fontName,int size, int slant, int offset);
export const PRN_IndBarcodeFont = (fontName, size, slant, offset, callback) => {
  return     sendEventToContentJs({"func":"PRN_IndBarcodeFont"
      , "para1":fontName, "para2":size, "para3":slant,"para4":offset
    }
    , callback);
}

//PRN_AddBarcodeToLabel (char* codeToPrint, char* codeType,
//int height, int posX, int posY, int dir,
//	int align, int enlargeWidth);
export const PRN_AddBarcodeToLabel = (codeToPrint, codeType, height, posX, posY, dir, align, enlargeWidth, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddBarcodeToLabel"
      , "para1":codeToPrint, "para2":codeType, "para3":height,"para4":posX, "para5":posY, "para6":dir, "para7":align, "para8":enlargeWidth
    }
    , callback);
}

// PRN_AddImageToLabel (char* imgFilePath, int posX, int posY);
export const PRN_AddImageToLabel = (imgFilePath, posX, posY, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddImageToLabel"
      , "para1":imgFilePath, "para2":posX, "para3":posY
    }
    , callback);
}

//PRN_AddImageToLabelEx (char* imgFilePath, int posX,	int posY, int dir, int align);
export const PRN_AddImageToLabelEx = (imgFilePath, posX,	posY, dir, align, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddImageToLabelEx"
      , "para1":imgFilePath, "para2":posX, "para3":posY, "para4": dir, "para5": align
    }
    , callback);
}


//PRN_AddCommand (char* data);
export const PRN_AddCommand = (data, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddCommand"
      , "para1":data
    }
    , callback);
}

//PRN_ClearLabelBuffer(void);
export const PRN_ClearLabelBuffer = (callback) => {
  return     sendEventToContentJs({"func":"PRN_ClearLabelBuffer"
    }
    , callback);
}

//PRN_PrintLabel ( int numOfCopies );
export const PRN_PrintLabel = (count, callback) => {
  return     sendEventToContentJs({"func":"PRN_PrintLabel", "para1":count }, callback);
}

//PRN_GetDriverStatus ( int* pStatus);
export const PRN_GetDriverStatus = ( callback) => {
  return     sendEventToContentJs({"func":"PRN_GetDriverStatus"
    }
    , callback);
}

//PRN_GetPrinterStatus ( int* pStatus);
export const PRN_GetPrinterStatus = ( callback) => {
  return     sendEventToContentJs({"func":"PRN_GetPrinterStatus"
    }
    , callback);
}

//PRN_GetInfoString(PRN_INFO_TYPE type, char* pbuf, int* psize);
export const PRN_GetInfoString = (type, callback) => {
  return     sendEventToContentJs({"func":"PRN_GetInfoString"
      , "para1": type
    }
    , callback);
}

//PRN_GetInfoInt(PRN_INFO_TYPE type, int* pvalue);
export const PRN_GetInfoInt = (type, callback) => {
  return     sendEventToContentJs({"func":"PRN_GetInfoInt"
      , "para1": type
    }
    , callback);
}

////PRN_SetCfg(PRN_CFG_ID id, int value);
export const PRN_SetCfg = (id, value, callback) => {
  return     sendEventToContentJs({"func":"PRN_SetCfg"
      , "para1": id
      , "para2": value
    }
    , callback);
}

//PRN_AddBarcodeToLabelEx(char* codeToPrint, char* codeType,	int height, int posX, int posY, int dir,		int align, int enlargeWidth, int *piAdv);
export const PRN_AddBarcodeToLabelEx = (codeToPrint, codeType,	height, posX, posY, dir, align, enlargeWidth, iAdvArray, callback) => {
  return     sendEventToContentJs({"func":"PRN_AddBarcodeToLabelEx"
      , "para1":codeToPrint, "para2":codeType, "para3":height,"para4":posX, "para5":posY, "para6":dir, "para7":align, "para8":enlargeWidth, "para9":iAdvArray
    }
    , callback);
}






